package com.jsy.mini12306.order.aspect;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jsy.mini12306.UserService;
import com.jsy.mini12306.common.entity.Passenger;
import com.jsy.mini12306.common.entity.TicketOrder;
import com.jsy.mini12306.order.config.MailConf;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author: SongyangJi
 * @description:
 * @since: 2021/9/9
 */

@Aspect
@Component
public class MailAop {

    @Reference
    UserService userService;

    @Resource
    MailConf mailConf;

    @Pointcut("execution(void com.jsy.mini12306.TicketOrderService.saveTicketOrder" +
            "(com.jsy.mini12306.common.entity.TicketOrder)) && args(ticketOrder)")
    public void sendTicketOrderMailPointCut(TicketOrder ticketOrder) {
    }

    //  正常结束才发送邮件
    @AfterReturning(value = "sendTicketOrderMailPointCut(ticketOrder)", argNames = "ticketOrder")
    public void doSendTicketOrderMailPointCut(TicketOrder ticketOrder) {
//        System.out.println("AOP");
//        System.out.println("userService"+userService);
        Long buyerId = ticketOrder.getBuyerId();
        Passenger passenger = userService.getEmailByPassengerId(buyerId);
//        System.out.println("passenger: " + passenger);
        String message = "尊敬的" + passenger.getName() + "用户，您已成功订购" + ticketOrder.getTrainName()
                + "在" + ticketOrder.getDepartDate() + "发车的车票。祝您旅途愉快";
        mailConf.sendEmail(passenger.getEmail(), "来自mini12306订单模块", message);

    }


}
